package com.hn.gege.sys.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.gege.sys.modules.sys.dto.JurisdictionTreeDTO;
import com.hn.gege.sys.modules.sys.entity.Jurisdiction;
import com.hn.gege.sys.modules.sys.service.IJurisdictionService;
import com.hn.gege.core.common.CommonPage;
import com.hn.gege.core.common.CommonResult;
import com.hn.gege.core.query.QueryGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;


@Api(tags = "SysJurisdictionController", description = "权限资源表管理")
@RestController
@RequestMapping("/sys/jurisdiction")
@Validated
public class JurisdictionController {

    @Autowired
    IJurisdictionService jurisdictionService;

    @ApiOperation("权限资源表分页列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageInfo")
    public CommonPage<Jurisdiction> pageInfo(@NotNull Integer pageNo,
                                             @NotNull Integer pageSize,
                                             Jurisdiction jurisdiction,
                                             HttpServletRequest request){
        Page<Jurisdiction> pageInfo = new Page<>(pageNo, pageSize);
        //构造查询条件
        QueryWrapper<Jurisdiction> qw = QueryGenerator.initQueryWrapper(jurisdiction, request.getParameterMap());
        Page<Jurisdiction> page1 = jurisdictionService.page(pageInfo, qw);
        return CommonPage.restPage(page1);
    }

    @ApiOperation("权限资源表树形列表")
    @GetMapping("/tree")
    public CommonResult< List<JurisdictionTreeDTO>> tree(){
        List<JurisdictionTreeDTO> tree = jurisdictionService.tree();
        return CommonResult.success(tree);
    }



    @ApiOperation("通过主键获取权限资源表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/{id}")
    public CommonResult<Jurisdiction> getByPk(@NotBlank @PathVariable("id") String id){
        Jurisdiction jurisdiction = jurisdictionService.getByPk(id);
        return CommonResult.success(jurisdiction);
    }

    @ApiOperation("新增权限资源表")
    @PostMapping
    public CommonResult add(@Validated Jurisdiction jurisdiction, BindingResult bindingResult){
    boolean b = jurisdictionService.save(jurisdiction);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("修改权限资源表")
    @PutMapping
    public CommonResult updateByPk(@Validated Jurisdiction jurisdiction, BindingResult bindingResult){
        boolean b = jurisdictionService.updateByPk(jurisdiction);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("通过主键删除权限资源表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public CommonResult removeByPk(@NotBlank @PathVariable("id") String id){
        boolean b = jurisdictionService.removeByPk(id);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }



}
